﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Pages_Vote_consume : System.Web.UI.Page
{
    DB DBManager = new DB();
    private bool flag = false;
    int[] voteIden;
    int[] voteKind;
    int length;
    string resultIp;
    protected void Page_Load(object sender, EventArgs e)
    {
        string sqlcon1 = "select id,voteMultiple from voteMaster";
        string sqlcon2 = "select count(*) from voteMaster";
        length = Convert.ToInt32(DBManager.executeGetReturn(sqlcon2));
        voteIden = new int[length];
        voteKind = new int[length];
        SqlDataReader sdr = DBManager.getSdr(sqlcon1);
        int j = 0;
        while (sdr.Read())
        {
            voteIden[j] = sdr.GetInt32(0);
            voteKind[j] = sdr.GetInt32(1);
            j = j + 1;
        }
        sdr.Close();
        for (int i = 0; i < length; i++)
        {
            if (voteKind[i] == 1)
            {
                TableRow tr1 = new TableRow();
                TableCell tc1 = new TableCell();
                tc1.HorizontalAlign = HorizontalAlign.Left;
                Label title = new Label();
                title.ID = "title" + voteIden[i].ToString();
                string sqlcon = "select voteTitle from voteMaster where id=" + voteIden[i];
                title.Text = (i + 1).ToString() + ". " + DBManager.executeGetReturn(sqlcon);
                tc1.Controls.Add(title);
                tr1.Cells.Add(tc1);
                Table1.Rows.Add(tr1);
                TableRow tr2 = new TableRow();
                TableCell tc2 = new TableCell();
                sqlcon = "select voteDetailsID,voteItem from voteDetails where id=" + voteIden[i];
                SqlDataReader sdr1 = DBManager.getSdr(sqlcon);
                CheckBoxList cblVote = new CheckBoxList();
                cblVote.ID = "cblVote" + voteIden[i].ToString();
                cblVote.DataSource = sdr1;
                cblVote.DataTextField = "voteItem";
                cblVote.DataValueField = "voteDetailsID";
                cblVote.RepeatDirection = RepeatDirection.Horizontal;
                cblVote.RepeatColumns = 4;
                cblVote.DataBind();
                sdr1.Close();
                tc2.Controls.Add(cblVote);
                tr2.Cells.Add(tc2);
                Table1.Rows.Add(tr2);

            }
            else
            {
                TableRow tr1 = new TableRow();
                TableCell tc1 = new TableCell();
                tc1.HorizontalAlign = HorizontalAlign.Left;
                Label title = new Label();
                title.ID = "title" + voteIden[i].ToString();
                string sqlcon = "select voteTitle from voteMaster where id=" + voteIden[i];
                title.Text = (i + 1).ToString() + ". " + DBManager.executeGetReturn(sqlcon);
                tc1.Controls.Add(title);
                tr1.Cells.Add(tc1);
                Table1.Rows.Add(tr1);
                TableRow tr2 = new TableRow();
                TableCell tc2 = new TableCell();
                sqlcon = "select voteDetailsID,voteItem from voteDetails where id=" + voteIden[i];
                SqlDataReader sdr1 = DBManager.getSdr(sqlcon);
                RadioButtonList rblVote = new RadioButtonList();
                rblVote.ID = "rblVote" + voteIden[i].ToString();
                rblVote.DataSource = sdr1;
                rblVote.DataTextField = "voteItem";
                rblVote.DataValueField = "voteDetailsID";
                rblVote.DataBind();
                sdr1.Close();
                tc2.Controls.Add(rblVote);
                tr2.Cells.Add(tc2);
                Table1.Rows.Add(tr2);
            }
        }
        TableRow tr3 = new TableRow();
        TableCell tc3 = new TableCell();
        Button submit = new Button();
        submit.ID = "btnSubmit";
        submit.Text = "提交";
        submit.Click += new EventHandler(btnSubmit_Click);
        Button result = new Button();
        result.ID = "btnSesult";
        result.Text = "结果";
        result.Click += new EventHandler(btnResult_Click);
        Label message = new Label();
        message.ID = "lblMessage";
        message.Text = "";
        tc3.Controls.Add(submit);
       // tc3.Controls.Add(result);
         tc3.Controls.Add(message);
        tr3.Cells.Add(tc3);
        Table1.Rows.Add(tr3);
    }

    public bool checkIP()
    {
        string sqlcon = "select voteIP from voteConfig";
        int checkedIP = Convert.ToInt32(DBManager.executeGetReturn(sqlcon));
        if (checkedIP > 0)
        {
            flag = true;
        }
        else
        {
            flag = false;
        }
        return flag;
    }

    public bool checkVoteIP()
    {
        string ip = Convert.ToString(resultIp);
        string sqlcon = "select count(*) from voter where ip='" + ip + "'";
        int result = Convert.ToInt32(DBManager.executeGetReturn(sqlcon));

        if (result > 0)
        {
            flag = true;
        }
        else
        {
            flag = false;
        }
        return flag;
    }

    public bool checkIPTime()
    {
        string getTime = "select voteTime from voteConfig";
        int checkTime = Convert.ToInt32(DBManager.executeGetReturn(getTime));

        string sqlcon = "select datediff(s,voterTime,getdate()) from voter where ip='" + resultIp + "'";
        long voterTime = Convert.ToInt64(DBManager.executeGetReturn(sqlcon));
        if (voterTime < checkTime)
        {
            flag = true;
        }
        else
        {
            flag = false;
        }
        return flag;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string[] selected;
        string sqlcon = "select count(voteDetailsID) from voteDetails";
        resultIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (null == resultIp || resultIp == String.Empty)
        {
            resultIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        if (null == resultIp || resultIp == String.Empty)
        {
            resultIp = HttpContext.Current.Request.UserHostAddress;
        }
        int sumOfChoice = Convert.ToInt32(DBManager.executeGetReturn(sqlcon));
        int numOfSelected = 0;
        selected = new string[sumOfChoice];
        string getTime = "select voteTime from voteConfig";
        int checkTime = Convert.ToInt32(DBManager.executeGetReturn(getTime));
        for (int i = 0; i < length; i++)
        {
            if (voteKind[i] == 1)
            {
                CheckBoxList cbl = (CheckBoxList)FindControl("cblVote" + voteIden[i].ToString());
                for (int n = 0; n < cbl.Items.Count; n++)
                {
                    if (cbl.Items[n].Selected == true)
                    {
                        selected[numOfSelected] = cbl.Items[n].Value;
                        numOfSelected = numOfSelected + 1;
                    }
                }
            }
            else
            {
                RadioButtonList rbl = (RadioButtonList)FindControl("rblVote" + voteIden[i].ToString());
                for (int n = 0; n < rbl.Items.Count; n++)
                {
                    if (rbl.Items[n].Selected == true)
                    {
                        selected[numOfSelected] = rbl.Items[n].Value;
                        numOfSelected = numOfSelected + 1;
                    }
                }
            }
        }
        /*for (int i = 0; i < numOfSelected; i++)
        {
            Label lbl = (Label)FindControl("lblMessage");
            lbl.Text = lbl.Text +selected [i]+"|";
        }*/
        Label lbl = (Label)FindControl("lblMessage");
        if (checkIP())
        {
            //如果IP存在
            if (checkVoteIP())
            {
                if (checkTime == 0)
                {
                    lbl.Text = "您已投过票.";
                }
                else
                {
                    //如果IP的投票时间没有经过限定的时间   不可以投票
                    if (checkIPTime())
                    {
                        lbl.Text = "时间间隔不合要求.";
                    }
                    //如果IP的投票时间经过限定的时间   可以投票
                    else
                    {
                        //更新投票从表  项目表
                        // string sqlUpDateVoteDetails = "update voteDetails set voteNum=voteNum+1 where voteDetailsID=" + this.rblVote.SelectedValue;
                        //更新投票信息表
                        string sqlUpDateVoter = "update voter set voterTime=getdate(),voterNum=voterNum+1 where ip='" + resultIp.ToString() + "'";

                        string[] arrsql = new string[numOfSelected + 1];
                        for (int i = 0; i < numOfSelected; i++)
                        {
                            arrsql[i] = "update voteDetails set voteNum=voteNum+1 where voteDetailsID=" + selected[i];
                        }
                        arrsql[numOfSelected] = sqlUpDateVoter;
                        if (DBManager.executeTransaction(arrsql))
                        {
                            lbl.Text = "投票成功,谢谢您的再一次投票.";
                        }
                        else
                        {
                            lbl.Text = "错误出现----投票成功,谢谢您的再一次投票.";
                        }
                    }
                }
            }
            //如果IP不存在
            else
            {
                //更新投票从表  项目表
                // string sqlUpDateVoteDetails = "update voteDetails set voteNum=voteNum+1 where voteDetailsID=" + this.rblVote.SelectedValue;
                //添加新的信息到投票信息表
                string sqlUpDateVoter = "insert into voter(ip,voterTime,voterNum) values('" + resultIp + "',getdate(),1)";

                string[] arrsql = new string[numOfSelected + 1];
                for (int i = 0; i < numOfSelected; i++)
                {
                    arrsql[i] = "update voteDetails set voteNum=voteNum+1 where voteDetailsID=" + selected[i];
                }
                arrsql[numOfSelected] = sqlUpDateVoter;
                if (DBManager.executeTransaction(arrsql))
                {
                    lbl.Text = "这是您的第一次投票,投票成功.";
                }
                else
                {
                    lbl.Text = "错误出现----这是您的第一次投票,投票成功.";
                }
            }

        }
        else//如果不限制IP投票
        {

            //更新投票从表  项目表
            //string sqlUpDateVoteDetails = "update voteDetails set voteNum=voteNum+1 where voteDetailsID=" + this.rblVote.SelectedValue;
            //如果投票者的IP存在  投票者已经投过票
            if (checkVoteIP())
            {
                //更新投票信息表
                string sqlUpDateVoter = "update voter set voterTime=getdate(),voterNum=voterNum+1 where ip='" + resultIp.ToString() + "'";

                string[] arrsql = new string[numOfSelected + 1];
                for (int i = 0; i < numOfSelected; i++)
                {
                    arrsql[i] = "update voteDetails set voteNum=voteNum+1 where voteDetailsID=" + selected[i];
                }
                arrsql[numOfSelected] = sqlUpDateVoter;
                if (DBManager.executeTransaction(arrsql))
                {
                    lbl.Text = "投票成功,谢谢您的再一次投票,系统不限制IP.";
                }
                else
                {
                    lbl.Text = "错误出现----投票成功,谢谢您的再一次投票,系统不限制IP.";
                }
            }
            //如果投票者的IP不存在  投票者第一次投票
            //更新投票从表  项目表
            else
            {
                //添加新的信息到投票信息表
                string sqlUpDateVoter = "insert into voter(ip,voterTime,voterNum) values('" + resultIp + "',getdate(),1)";
                string[] arrsql = new string[numOfSelected + 1];
                for (int i = 0; i < numOfSelected; i++)
                {
                    arrsql[i] = "update voteDetails set voteNum=voteNum+1 where voteDetailsID=" + selected[i];
                }
                arrsql[numOfSelected] = sqlUpDateVoter;
                if (DBManager.executeTransaction(arrsql))
                {
                    lbl.Text = "这是您的第一次投票,投票成功,系统不限制IP.";
                }
                else
                {
                    lbl.Text = "错误出现----这是您的第一次投票,投票成功,系统不限制IP.";
                }
            }
        }

    }
    protected void btnResult_Click(object sender, EventArgs e)
    {
        Response.Redirect("showResult.aspx");
    }
}
